import World3d from "./src/3d/scene/World3d.js";
import {assets3dBaseUrl, KeyboardState, KeyCode} from "./src/3d/lib/CommonConsts.js";
import World3dEvent from "./src/3d/scene/event/World3dEvent.js";
import PathCompute from "./src/3d/pathCompute/PathCompute.js";
import PathComputeUI from "./src/3d/pathCompute/PathComputeUI.js";
import {ConstraintEnum} from "./src/3d/plugins/RoutePointPlugin.js";
import {IsoEnum} from "./src/3d/model/IsoLineVO.js";

const qtChannelUrl = 'ws://localhost:11111';
// const qtChannelUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]);
// const testUrl = 'http://117.176.159.182:8088/upload/upload/280/3880df1851fb1206a6fb28cd9661ef13a93cf852/terra_b3dms/tileset.json';
// const testUrl = './static/b3dms/tashui_simple/tileset.json';
// const testUrl = './static/b3dms/small_0523/tileset.json';
// const testUrl = './static/b3dms/qinbaijiang_b3dms/tileset.json';
// const testUrl = './static/b3dms/gymnasium_0523/tileset.json';
const testUrl = './static/b3dms/gymnasium_test/tileset.json';
// const testUrl = 'http://aipds.aigeltech.com/upload/upload/344/9cbbe854b27c591984dd0bd2a9f3b63e1b9b3109/terra_b3dms/tileset.json';
// const testUrl = './static/b3dms/b88_drawson_rd/tileset.json';
// const testRouteUrl = '../json/route_3d_anim.json';
const testRouteUrl = '../json/route_711.json';
// const projectPlanUrl = 'https://hyl-common.oss-cn-chengdu.aliyuncs.com/aos_drone1.json';
const projectPlanUrl = assets3dBaseUrl + 'plan/plan1.json';
let world3d
const box3d = document.getElementById('box');

function initWorld3d() {
  world3d = new World3d();
  world3d.init(document.getElementById('playground'));
  // world3d.initQtSocket(qtChannelUrl);
  world3d.startRender();
  world3d.loadB3dmsModel(testUrl); // 直接加载 b3dms 3d tiles
 // world3d.loadEditData(projectPlanUrl);
 //  world3d.testDroneFlyPath()
  world3d.loadFromLocalStorage();
  // world3d.loadSprayPaths(testRouteUrl);
  // world3d.loadFromRemote(WorldConfig.testedProjectId)
  // world3d.testDroneFlyPath()
}

initWorld3d();

box3d.addEventListener('pointerdown', (event) => {
  world3d.pointerDown(event);
});

box3d.addEventListener('pointermove', (event) => {
  world3d.pointerMove(event);
});

box3d.addEventListener('pointerup', (event) => {
  world3d.pointerUp(event);
});

const editPathBtn = document.getElementById('editPathBtn')
const editPathsBtn = document.getElementById('editPathsBtn')
const playBtn = document.getElementById('playBtn')

editPathBtn && editPathBtn.addEventListener('click', () => {
  world3d.editingSprayPath(true)
})
editPathsBtn && editPathsBtn.addEventListener('click', () => {
  world3d.editingSprayPath(false)
})
playBtn && playBtn.addEventListener('click', () => {
  world3d.showCustomSprayPathAnimation()
})


// init UI
const colorsData = [
  // yellow
  ['87', '08E51', '#eea814'],
  ['88', '08E53', '#feb402'],
  ['81', '08C40', '#5c5146'],
  ['103', '10B27', '#5c5343'],
  ['109', '10C39', '#66583e'],
  ['80', '08C39', '#6d553c'],
  ['85', '08D45', '#8c6439'],
  ['79', '08C37', '#9f7944'],
  ['84', '08D44', '#ad7b36'],
  ['90', '08E56', '#cc8d26'],
  ['83', '08D43', '#c8903c'],
  ['82', '08D41', '#efc484'],
  ['86', '08E49', '#eed7b6'],
  ['116', '10E51', '#e9bf28'],
  ['118', '10E55', '#f9cd1b'],
  ['117', '10E53', '#eecd10'],
  ['115', '10E50', '#f3d66b'],
  ['110', '10D41', '#f0d684'],
  ['108', '10C37', '#94803e'],
  ['113', '10D45', '#988237'],
  ['112', '10D44', '#b69731'],
  ['119', '10E56', '#d1ab17'],
  ['111', '10D43', '#c6a93b'],
  ['107', '10C35', '#c1ab68'],
  ['114', '10E49', '#e9e2a6'],
  ['128', '12C31', '#e7e7c1'],
  ['102', '10B25', '#756f5c'],
  ['101', '10B23', '#8d856e'],
  ['100', '10B21', '#a39b84'],
  ['99', '10B19', '#bbb196'],
  ['98', '10B17', '#d3c9ac'],
  ['120', '12B15', '#e3e3cf'],
  ['106', '10C33', '#e7d59e'],
  ['105', '10C31', '#f0e2c1'],
  ['76', '08C31', '#f0dfc4'],
  ['97', '10B15', '#f0e7d5'],
  // Orange
  ['65', '06E55', '#e86833'],
  ['64', '06E53', '#f78920'],
  ['89', '08E55', '#fe9f0d'],
  ['63', '06E51', '#f7935b'],
  ['62', '06E50', '#febc79'],
  ['56', '06C39', '#76533d'],
  ['61', '06D45', '#955d3f'],
  ['55', '06C37', '#aa7455'],
  ['78', '08C35', '#d5a972'],
  ['66', '06E56', '#bd6c37'],
  ['60', '06D44', '#b87042'],
  ['59', '06D43', '#cf8746'],
  ['54', '06C35', '#e1a77e'],
  ['58', '06D41', '#f9bf93'],
  ['53', '06C33', '#e5c29f'],
  ['77', '08C33', '#e1c29c'],
  ['75', '08B29', '#534b44'],
  ['74', '08B27', '#625248'],
  ['73', '08B25', '#78695e'],
  ['72', '08B23', '#8c7e71'],
  ['71', '08B21', '#a49587'],
  ['70', '08B19', '#b7aa99'],
  ['69', '08B17', '#d8c8b4'],
  ['68', '08B15', '#f1e6d5'],
  // green
  ['140', '12E55', '#73a647'],
  ['139', '12E53', '#98b747'],
  ['138', '12E51', '#c9d047'],
  ['134', '12D41', '#cad27c'],
  ['129', '12C33', '#c9cc96'],
  ['121', '12B17', '#c9cbac'],
  ['132', '12C39', '#535b3f'],
  ['137', '12D45', '#6a713e'],
  ['131', '12C37', '#7b8546'],
  ['136', '12D44', '#808738'],
  ['135', '12D43', '#8f9942'],
  ['130', '12C35', '#adb480'],
  ['156', '14E58', '#375248'],
  ['145', '14C39', '#425f52'],
  ['144', '14C37', '#648e78'],
  ['148', '14D43', '#4b8f70'],
  ['147', '14D41', '#8cbda1'],
  ['150', '14D45', '#2b604c'],
  ['149', '14D44', '#38795d'],
  ['155', '14E56', '#017d58'],
  ['154', '14E53', '#08936d'],
  ['153', '14E51', '#55ad79'],
  ['152', '14E50', '#a9d8ba'],
  ['151', '14E49', '#cfead6'],
  ['161', '16C35', '#9bb6b1'],
  ['160', '16C33', '#b3cdc9'],
  ['143', '14C35', '#9db4a3'],
  ['142', '14C33', '#b5cfbf'],
  ['141', '14C31', '#d7e5d7'],
  ['168', '16D45', '#1d5f61'],
  ['167', '16D44', '#1c7a79'],
  ['166', '16D43', '#378f8d'],
  ['171', '16E56', '#007a7a'],
  ['170', '16E53', '#039092'],
  ['165', '16D41', '#81bcb7'],
  ['169', '16E50', '#99d6d0'],
  ['163', '16C39', '#315e60'],
  ['162', '16C37', '#578988'],
  ['187', '18D41', '#87bcc8'],
  ['164', '16C40', '#404b4c'],
  ['146', '14C40', '#414d46'],
  ['159', '16A11', '#656c6b'],
  ['158', '16A07', '#919694'],
  ['133', '12C40', '#454940'],
  ['127', '12B29', '#474a41'],
  ['126', '12B27', '#585b4d'],
  ['125', '12B25', '#6b6d5c'],
  ['124', '12B23', '#7f836e'],
  ['123', '12B21', '#999d83'],
  ['122', '12B19', '#b0b395'],
  ['157', '16A03', '#c6cbc2'],
  ['2', '00A03', '#c7c8c3'],
  ['175', '18B19', '#b1b9b9'],
  ['174', '18B17', '#c3cccb'],
  ['182', '18C33', '#baccce'],
  ['191', '18E49', '#d3e5e2'],
  ['181', '18C31', '#e6e8e2'],
  // Purple
  ['228', '22E58', '#4d4367'],
  ['226', '22D45', '#5b5184'],
  ['227', '22E53', '#6d6196'],
  ['225', '22D44', '#71689b'],
  ['220', '22C37', '#7d769b'],
  ['221', '22C39', '#595175'],
  ['224', '22D43', '#877fb0'],
  ['223', '22D41', '#afafd3'],
  ['219', '22C35', '#b0aec2'],
  ['218', '22C33', '#c7c5d5'],
  ['222', '22C40', '#4a4255'],
  ['216', '22B29', '#474451'],
  ['215', '22B27', '#5a5566'],
  ['214', '22B25', '#6a6773'],
  ['213', '22B23', '#82808a'],
  ['212', '22B21', '#96939c'],
  ['233', '24C40', '#514051'],
  ['232', '24C39', '#6a4d6d'],
  ['231', '24C37', '#9a7a97'],
  ['234', '24E50', '#e3c2d9'],
  ['237', '24E58', '#5b3d58'],
  ['236', '24E56', '#865086'],
  ['235', '24E53', '#8c5a8c'],
  ['230', '24C35', '#bfa9b9'],
  ['229', '24C33', '#d2c0cd'],
  ['211', '22B19', '#b2b2bc'],
  ['210', '22B17', '#c8c5ca'],
  // red
  ['42', '04D45', '#8e3d3d'],
  ['48', '04E56', '#bc373a'],
  ['46', '04E53', '#cc443c'],
  ['47', '04E55', '#cf4734'],
  ['41', '04D44', '#ab534e'],
  ['40', '04D43', '#c15f56'],
  ['45', '04E51', '#ee7563'],
  ['39', '04D41', '#e99d8d'],
  ['44', '04E50', '#febbad'],
  ['37', '04C39', '#7e4a46'],
  ['36', '04C37', '#ab6f67'],
  ['35', '04C35', '#e4a398'],
  ['34', '04C33', '#ebbdb4'],
  ['43', '04E49', '#f6d9d2'],
  ['17', '02C40', '#59434c'],
  ['38', '04C40', '#5a3f42'],
  ['49', '04E58', '#6e4146'],
  ['16', '02C39', '#7f4957'],
  ['15', '02C37', '#a86f7f'],
  ['24', '02E58', '#68394d'],
  ['21', '02D45', '#89395a'],
  ['23', '02E56', '#aa4567'],
  ['22', '02E53', '#ce5a7c'],
  ['20', '02D44', '#a94d69'],
  ['19', '02D43', '#c1647f'],
  ['18', '02D41', '#ee9ca8'],
  ['14', '02C35', '#dfa2ad'],
  ['13', '02C33', '#e7c0c4'],
  ['32', '04B29', '#544544'],
  ['57', '06C40', '#5c453e'],
  ['31', '04B27', '#6a5350'],
  ['30', '04B25', '#7e6763'],
  ['29', '04B23', '#947c75'],
  ['28', '04B21', '#ac948d'],
  ['27', '04B19', '#c5aca3'],
  ['26', '04B17', '#d8c3bb'],
  ['25', '04B15', '#edded6'],
  ['33', '04C31', '#f6e7e0'],
  // blue
  ['195', '18E58', '#2a4c61'],
  ['190', '18D45', '#235d75'],
  ['189', '18D44', '#38748d'],
  ['188', '18D43', '#47869d'],
  ['194', '18E53', '#00719e'],
  ['193', '18E51', '#41a1bf'],
  ['192', '18E50', '#abd2db'],
  ['208', '20E56', '#1b5d92'],
  ['207', '20E53', '#2c6eaa'],
  ['206', '20E51', '#659fd4'],
  ['185', '18C39', '#3a5a6a'],
  ['184', '18C37', '#5f8390'],
  ['183', '18C35', '#9bb1b9'],
  ['177', '18B23', '#7b8485'],
  ['176', '18B21', '#939c9d'],
  ['200', '20C40', '#384356'],
  ['199', '20C39', '#48566d'],
  ['204', '20D45', '#3a5b83'],
  ['203', '20D44', '#486d9e'],
  ['202', '20D43', '#5e84b3'],
  ['201', '20D41', '#9ab6d3'],
  ['205', '20E50', '#b6d1e9'],
  ['198', '20C37', '#6d85a2'],
  ['8', '00E53', '#3a3a3b'],
  ['180', '18B29', '#404448'],
  ['172', '18A14', '#464a4e'],
  ['7', '00A13', '#5a5b5c'],
  ['5', '00A09', '#818384'],
  ['186', '18C40', '#3d464d'],
  ['179', '18B27', '#525a5e'],
  ['178', '18B25', '#697176'],
  ['197', '20C35', '#a2b2c5'],
  ['196', '20C33', '#bbccdc'],
  // grey
  ['67', '08A14', '#464442'],
  ['104', '10B29', '#4a4741'],
  ['96', '10A11', '#696865'],
  ['6', '00A11', '#6a6a6a'],
  ['12', '02A11', '#716c6d'],
  ['52', '06A11', '#716f6e'],
  ['95', '10A09', '#878780'],
  ['51', '06A07', '#979692'],
  ['94', '10A07', '#9b9893'],
  ['11', '02A07', '#9e9b9a'],
  ['4', '00A07', '#9b9b9b'],
  ['3', '00A05', '#b0afae'],
  ['93', '10A05', '#b4b3a9'],
  ['92', '10A03', '#c6c5bd'],
  ['10', '02A03', '#ccc9c7'],
  ['50', '06A03', '#cfc9c1'],
  ['1', '00A01', '#d2d0ca'],
  ['217', '22C31', '#e4e2e1'],
  ['209', '22B15', '#e5e2de'],
  ['91', '10A01', '#eae9e1'],
  ['9', '00E55', '#f1f0e8'],
  ['173', '18B15', '#e3e6e0'],
];

/**
 * jscolor库拷贝; 参数r，g，b都是 0到255之间的整数
 * @param {number} r
 * @param {number} g
 * @param {number} b
 */
function isLightColor(r, g, b) {
  return (
    0.213 * r +
    0.715 * g +
    0.072 * b >
    255 / 2
  );
}

initDoms();

function initDoms() {
  const setPoints = document.getElementById('setPoints');
  const pickDirs = document.getElementById('pickDirs');
  const jsonInput = document.getElementById('jsonInput');
  const createItems = document.getElementById('createItems');
  const saveBtn = document.getElementById('saveBtn');
  const computeCornersBtn = document.getElementById('computeCornersBtn');
  const setStartEndBtn = document.getElementById('setStartEndBtn');
  const computePath = document.getElementById('computePath');
  const markRagFaces = document.getElementById('markRagFaces');
  const removeRagFaces = document.getElementById('removeRagFaces');
  const testRoute = document.getElementById('testRoute');
  const pathConstraints = document.getElementById('pathConstraints');
  const pointAdhere = document.getElementById('pointAdhere');
  const pointEdit = document.getElementById('pointEdit');
  const hOffset = document.getElementById('hOffset');
  // const cbAlignEye = document.getElementById('cbAlignEye');
  const cbShowConvex = document.getElementById('cbShowConvex');
  const rdH = document.getElementById('rdH');
  const rdV = document.getElementById('rdV');
  // const testFly = document.getElementById('testFly');
  const phiSlider = document.getElementById('phiSlider');
  const thetaSlider = document.getElementById('thetaSlider');

  const colorInput = document.querySelector('.search-color-plate');
  const colorBtn = document.querySelector('.icon-box');
  const colorBricksDom = document.getElementById('colorBricks');
  const colorPlateDom = document.querySelector('.color-plate');
  colorPlateDom.style.visibility = "hidden";

  colorBtn.onclick = applyColors;
  colorInput.onkeydown = enterSearchKeyword;
  colorInput.onfocus = initDropdownItems;
  colorInput.onblur = debouncedPressedEnter;

  world3d.scene.addEventListener(World3dEvent.clearEditorState.type, () => {
    setPoints.selectedIndex = 0
    pickDirs.selectedIndex = 0
    createItems.selectedIndex = 0
    if (pointEdit.selectedIndex === 1) {
      pointEdit.selectedIndex = 0
    }

  });
  world3d.scene.addEventListener(
    World3dEvent.workAreaSelected.type,
    () => {
      colorPlateDom.style.visibility = "";
    },
  );
  world3d.scene.addEventListener(
    World3dEvent.surfaceAreaSelected.type,
    () => {
      colorPlateDom.style.visibility = "";
    },
  );

  world3d.scene.addEventListener(
    World3dEvent.clearPaneSelection.type,
    () => {
      colorPlateDom.style.visibility = "hidden";
    },
  );

  function enterSearchKeyword() {
    console.log('enterSearchKeyword')
  }

  function initDropdownItems() {
    console.log('initDropdownItems')
    colorBricksDom.onclick = mouseclickColorPlate;
    colorBricksDom.style.visibility = "";
  }

  function debouncedPressedEnter() {
    console.log('debouncedPressedEnter')
    // colorBricksDom.style.visibility = "hidden";
  }

  function applyColors() {
    console.log('applyColors: ', selectingItem);
    if (selectingItem && selectingItem.length) {
      world3d.changeAreaColor(selectingItem[2]);
    }
  }

  function mouseclickColorPlate(event) {
    if (!event.target.className.startsWith('color-item-')) {
      return;
    }
    const info = event.target.childNodes[0].innerText;
    const arr = info.split(', ');
    selectingItem = arr;
    onColorSelected(arr);
  }

  const onColorSelected = (arr) => {
    const inputDom = colorInput;
    if (!inputDom) {
      return;
    }
    if (!arr) {
      inputDom.style.backgroundColor = '#fff';
      inputDom.style.color = '#000';
      // inputDom.value = searchInputStr;
    } else {
      inputDom.style.backgroundColor = arr[2];
      inputDom.style.color = arr[3];
      inputDom.value = arr[1];
    }
    colorBricksDom.style.visibility = "hidden";
  }

  /**
   * @param {string} cssColorStr, css 颜色字符串，如 ‘#fdfddf'
   * @return {boolean}
   */
  function isLightCssColor(cssColorStr) {
    const r = Number('0x' + cssColorStr.slice(1,3));
    const g = Number('0x' + cssColorStr.slice(3,5));
    const b = Number('0x' + cssColorStr.slice(5,7));
    return isLightColor(r, g, b);
  }

  const dropdownColors0 = [];
  let selectingItem = null;

  function initialize() {
    const codeColorMap = new Map();
    const wrongKeys = [];
    colorsData.forEach(arr => {
      const arrayItem = arr.slice();
      const cssColor = arr[2];
      arrayItem[3] = isLightCssColor(cssColor) ? '#000000' : '#ffffff';
      arrayItem[2] = arr[2].toUpperCase();
      dropdownColors0.push(arrayItem);
      const key = arrayItem[1];
      const color = arrayItem[2];
      if (codeColorMap.has(key)) {
        codeColorMap.get(key).push(color)
        wrongKeys.push({key, arr: codeColorMap.get(key)});
      } else {
        codeColorMap.set(key, [color]);
      }
    });
    initColorsDropdown();
  }
  function initColorsDropdown() {
    dropdownColors0.forEach(arr => {
      const child = document.createElement('div');
      child.className = arr[3] == "#000000" ? "color-item-dark" : "color-item-light";
      child.style.backgroundColor = arr[2];
      child.style.color = arr[3];
      child.id = 'color_' + arr[0];
      child.innerHTML = `<li hidden>${arr.join(', ')}</li> ${arr[2]}`;
      colorBricksDom.appendChild(child);
    });
    colorBricksDom.style.visibility = "hidden";
  }
  initialize()

  setPoints.onchange = () => {
    switch (setPoints.selectedIndex) {
      case 1: // 设置相机焦点
        world3d.startSetTarget()
        break;
      case 2: // 设置无人机起飞点
        world3d.startSetFlyHomePoint();
        break;
      case 3: // 设置无人机校准点
        world3d.startSetCalibration();
        break;
      default:
        break;
    }
  }

  pickDirs.onchange = (event) => {
    switch (pickDirs.selectedIndex) {
      case 1: // 设置采样空间
        world3d.settingOrientSampleCube();
        break;
      case 2: // 采样凸包轮廓
        if (world3d.createOrientationContour()) {
          world3d.settingOrientation();
        }
        break;
      case 3: // 结束方位采样
        world3d.endAddOrientaion();
        break;
      case 4: // 找辅助平面(立面）
        world3d.settingPlaneHelper(true);
        break;
      case 5: // 找辅助平面(倾斜面）
        world3d.settingPlaneHelper(false);
        break;
      case 6: // 导出辅助平面
        world3d.store.exportPlaneRefs();
        break;
      case 7: // 导入辅助平面
        jsonInput.click();
        break;
      case 8: // 选定非平面工作区 在不规则曲面上标记工作区
        world3d.addingSliceLines();
        break;
      case 9: // 设置三维凸包采样空间
        world3d.addingConvex3dVert();
        break;
      case 10: // 采样三维凸包网格
        // world3d._addIsoLinePlugin.sampleOutConvexMesh()
        world3d._surfaceGridPlugin.sampleOutConvexMesh()
        break;
      default:
        world3d.endAddOrientaion();
        break;
    }
  }

  jsonInput.oninput = (event) => {
    world3d.store.importPlaneRefs(event.target.files[0]);
  }

  createItems.onchange = (event) => {
    switch (createItems.selectedIndex) {
      case 1: // 创建工作区
        world3d.addingWorkArea();
        break;
      case 2: // 创建不可喷涂区
        world3d.addingHole();
        break;
      case 3: // 编辑不可喷涂区
        world3d.editingHole();
        break;
      case 4: // 标定障碍物
        world3d.startAddBlocking();
        break;
      case 5: // 标定航点
        world3d.startAddRoutePoints()
        break;
      // case 6: // 标定局部切平面
      //   world3d.startAddNormalPoints()
      //   break;
      case 6: // 标定纬度等值线
        world3d.startAddIsoLine(IsoEnum.phi)
        break;
      case 7: // 标定经度等值线
        world3d.startAddIsoLine(IsoEnum.theta)
        break;
      case 8: // 给选中的等值线追加采样点
        world3d.startAppendPointsToIsoLine()
        break;
      case 9: // 新增曲面网格顶点
        world3d.store.addingGridVert()
        break;
      default:
        world3d.endAddOrientaion();
        break;
    }
  }

  pathConstraints.onchange = (event) => {
    switch (pathConstraints.selectedIndex) {
      case 1: // 水平
        world3d.setRouteConstraintPlane(ConstraintEnum.horizontal)
        break;
      case 2: // 竖直
        world3d.setRouteConstraintPlane(ConstraintEnum.vertical)
        break;
      case 3: // 特定角度
        world3d.setRouteConstraintPlane(ConstraintEnum.custom)
        break;
      default:
        // 无约束
        world3d.setRouteConstraintPlane(ConstraintEnum.none)
        break;
    }
  }
  pointAdhere.onchange = (event) => {
    switch (pointAdhere.selectedIndex) {
      case 1: // 新增选中航点的水平吸附面
        world3d.addRouteAdherePlane(ConstraintEnum.horizontal)
        pointAdhere.selectedIndex = 0
        break;
      case 2: // 新增选中航点的竖直吸附面
        world3d.addRouteAdherePlane(ConstraintEnum.vertical)
        pointAdhere.selectedIndex = 0
        break;
      case 3: // 新增特定角度的吸附平面
        world3d.addRouteAdherePlane(ConstraintEnum.custom)
        pointAdhere.selectedIndex = 0
        break;
      case 4: // 清除所有新增的吸附平面
        world3d.clearRouteAdherePlanes()
        break;
      default:
        // 无约束
        world3d.clearRouteAdherePlanes()
        break;
    }
  }
  pointEdit.onchange = (event) => {
    switch (pointEdit.selectedIndex) {
      case 1: // 新增航线
        world3d.addNewRoute()
        createItems.selectedIndex = 5
        // world3d.start
        break;
      case 2: // 从选中航点的尾新增
        world3d.appendRoutePoint(false)
        break;
      case 3: // 从选中航点的首新增
        world3d.appendRoutePoint(true)
        break;
      case 4: // 从选中法向采样点的尾新增
        // world3d._addIsoLinePlugin.insertNormalPoint()
        break;
      default:
        break;
    }
    pointEdit.selectedIndex = 0
  }
  hOffset.oninput = (event) => {
    world3d.updateRouteOffset(event.target.value)
  }
  rdH.onclick = (event) => {
    world3d.updateSingleRouteOffsetType(event.target.value)
  }
  rdV.onclick = (event) => {
    world3d.updateSingleRouteOffsetType(event.target.value)
  }
  // cbAlignEye.onclick = (event) => {
  //   world3d.store.setAlignToViewport(event.target.checked)
  // }
  phiSlider.onclick = (event) => {
    world3d.updatePhiRange(0, event.target.value)
  }
  thetaSlider.onclick = (event) => {
    world3d.updateThetaRange(0, event.target.value)
  }
  cbShowConvex.onclick = (event) => {
    world3d.toggleConvexVisible(event.target.checked)
  }

  saveBtn.onclick = () => {
    PathComputeUI.clearCornersAndAreas(world3d.store)
    world3d.saveToLocalStorage()
  }

  computeCornersBtn.onclick = () => {
    const selectAreaMesh = world3d.store.pickingArea();
    if (!selectAreaMesh) {
      return;
    }
    PathComputeUI.clearCornersAndAreas(world3d.store);
    const blockingArr = world3d.store.blockings.map(it => it.vo);
    const areaArr = world3d.store.areaMeshes.map(it => it.vo);
    const pathCompute = new PathCompute(blockingArr, areaArr);
    const cornerResult = pathCompute.findCornersAndSprayAreas(selectAreaMesh.vo, world3d.scene);
    console.log(cornerResult);
    PathComputeUI.displayCornersAndAreas(world3d.scene, selectAreaMesh, cornerResult);
  }

  setStartEndBtn.onclick = () => {
    console.log('preparing for path compute');
    world3d.startMovingStartEnd();
  }
  computePath.onclick = () => {
    console.log('执行三维避障寻路算法');
    const blockingArr = world3d.store.blockings.map(it => it.vo);
    const areaArr = world3d.store.areaMeshes.map(it => it.vo);
    const pathCompute = new PathCompute(blockingArr, areaArr);
    const workAreaMesh = PathComputeUI.getSelectAreaMesh();
    if (!workAreaMesh) {
      console.log("必须先指定一个工作区")
    }
    PathComputeUI.clearDroneSprayPaths(workAreaMesh);
    const indicators = PathComputeUI.getDroneStartAndEnd();
    const srcWorldPos = indicators[0].position;
    const dstWorldPos = indicators[1].position;
    const paths = pathCompute.findDronePath(workAreaMesh.vo, srcWorldPos, dstWorldPos);
    PathComputeUI.displayDronePaths(world3d.scene, paths);
  }
  markRagFaces.onclick = () => {
    const shapeTool = world3d.editOrientation.shapeExtract;
    shapeTool.markRagFaces();
  }
  removeRagFaces.onclick = () => {
    const shapeTool = world3d.editOrientation.shapeExtract;
    shapeTool.removeRagFaces();
  }
  testRoute.onclick = () => {
    const shapeTool = world3d.editOrientation.shapeExtract;
    shapeTool.testRoute();
  }

  // testFly.onclick = () => {
  //   world3d.testDroneFlyPath()
  // }

  window.addEventListener('keydown', (event) => {
    // console.log("keydown ", event.code)
    switch (event.code) {
      case KeyCode.Digit1:
        PathComputeUI.showSinglePath(true);
        break;
      case KeyCode.Digit2:
        PathComputeUI.showSinglePath(false);
        break;
      case KeyCode.Digit3:
        PathComputeUI.resetCurPathIndex();
        break;
      case KeyCode.Digit4:
        PathComputeUI.moveStartDrone(true);
        break;
      case KeyCode.Digit5:
        PathComputeUI.moveStartDrone(false);
        break;
      case KeyCode.Digit6:
        PathComputeUI.clearCornersAndAreas(world3d.store);
        break;
    }
  });
}


